package edu.gatech.interaction;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;

class SpaceTravelMiniGame
{
	private int yPos;
	private boolean gameOver, win, pirateHit;
	private boolean shipMovingUp, shipMovingDown;
	private int goal;
	private float timeCounter;
	private ArrayList<Asteroid> asteroidList;
	private ArrayList<PirateShip> pirateShipList;
	
	public void start(int goal)
	{
		yPos = 50;
		gameOver = false;
		win = false;
		pirateHit = false;
		shipMovingUp = false;
		shipMovingDown = false;
		this.goal = goal;
		timeCounter = 0;
		asteroidList = new ArrayList<Asteroid>();
		pirateShipList = new ArrayList<PirateShip>();
	}
	
	public void step()
	{
		if(shipMovingUp)
		{
			shipMovingUp = false;
			if(yPos > 80)
				yPos -= 10;
			else
				yPos = 70;
		}
		else if(shipMovingDown)
		{
			shipMovingDown = false;
			if(yPos < 430)
				yPos += 10;
			else
				yPos = 440;
		}
		for(Asteroid a : asteroidList)
			a.move();
		for(PirateShip p : pirateShipList)
			p.move();
		timeCounter += .05;
		if( (timeCounter-(int)(timeCounter)) <= .05 && (timeCounter - (int)(timeCounter)) >= -.05)
		{
			int rand = new Random().nextInt(100);
			if(rand <= 10)
				pirateShipList.add(new PirateShip(new Random().nextInt(370) + 70));
			else
				asteroidList.add(new Asteroid(new Random().nextInt(370) + 70));
		}
		for(int i = asteroidList.size() - 1; i >= 0; i--)
		{
			if(asteroidList.get(i).isDestroyed())
			{
				asteroidList.remove(i);
			}
		}
		for(int i = pirateShipList.size() - 1; i >= 0; i--)
		{
			if(pirateShipList.get(i).isDestroyed())
			{
				pirateShipList.remove(i);
			}
		}
		for(Asteroid a : asteroidList)
			if(collision(a))
			{
				gameOver = true;
				pirateHit = false;
				win = false;
			}
		for(PirateShip p : pirateShipList)
			if(collision(p))
			{
				gameOver = true;
				pirateHit = true;
				win = false;
			}
		
		if((int)timeCounter >= goal)
		{
			gameOver = true;
			win = true;
		}
	}
	
	private boolean collision(Asteroid a)
	{
		int ax1 = a.getXPos();
		int ay1 = a.getYPos();
		int ax2 = ax1 + 50;
		int ay2 = ay1 + 50;
		
		int sx1 = 50;
		int sy1 = yPos;
		int sx2 = sx1 + 200;
		int sy2 = sy1 + 50;
		
		if (ax1 < sx2 && ax2 > sx1 && ay1 < sy2 && ay2 > sy1)
			return true;
		return false;
	}
	private boolean collision(PirateShip p)
	{
		int px1 = p.getXPos();
		int py1 = p.getYPos();
		int px2 = px1 + 200;
		int py2 = py1 + 160;
		
		int sx1 = 50;
		int sy1 = yPos;
		int sx2 = sx1 + 200;
		int sy2 = sy1 + 50;
		
		if (px1 < sx2 && px2 > sx1 && py1 < sy2 && py2 > sy1)
			return true;
		return false;
	}
	
	public void moveShipUp()
	{
		shipMovingUp = true;
	}
	
	public void moveShipDown()
	{
		shipMovingDown = true;
	}
	
	public void draw(Graphics2D g2d, ImageObserver io)
	{
		ImageIcon background = new ImageIcon("src/edu/gatech/interaction/ST_Minigame_Spaceship.PNG");
		Image image = background.getImage();
		g2d.drawImage(image, 50, yPos, io);
		
		for(Asteroid a : asteroidList)
			a.draw(g2d, io);
		for(PirateShip p : pirateShipList)
			p.draw(g2d, io);

		g2d.setColor(Color.WHITE);
		g2d.setFont(new Font("Trebuchet MS", Font.PLAIN, 50));
		g2d.drawString("Dodge the Asteroids!", 175, 50);
		
		g2d.setFont(new Font("Trebuchet MS", Font.PLAIN, 30));
		g2d.drawString("Time: " + (int)timeCounter + "s", 100, 550);
		g2d.drawString("Goal: " + goal + "s", 550, 550);
	}
	
	public boolean isGameOver()
	{
		return gameOver;
	}
	
	public boolean isPirateHit()
	{
		return pirateHit;
	}
	
	public boolean isWin()
	{
		return win;
	}
	
	private class Asteroid
	{
		private int xPos, yPos;
		private boolean destroyed;
		
		public Asteroid(int yPos)
		{
			xPos = 750;
			this.yPos = yPos;
			destroyed = false;
		}
		
		public void move()
		{
			xPos -= 10;
			if(xPos <= 0)
				destroyed = true;
		}
		
		public void draw(Graphics2D g2d, ImageObserver io)
		{
			ImageIcon background = new ImageIcon("src/edu/gatech/interaction/ST_Minigame_Asteroid.PNG");
			Image image = background.getImage();
			g2d.drawImage(image, xPos, yPos, io);
		}
		
		public boolean isDestroyed()
		{
			return destroyed;
		}
		
		public int getXPos()
		{
			return xPos;
		}
		
		public int getYPos()
		{
			return yPos;
		}
	}
	
	private class PirateShip
	{
		private int xPos, yPos;
		private boolean destroyed;
		
		public PirateShip(int yPos)
		{
			xPos = 750;
			this.yPos = yPos;
			destroyed = false;
		}
		
		public void move()
		{
			xPos -= 20;
			if(xPos <= 0)
				destroyed = true;
		}
		
		public void draw(Graphics2D g2d, ImageObserver io)
		{
			ImageIcon background = new ImageIcon("src/edu/gatech/interaction/ST_Minigame_Pirateship.PNG");
			Image image = background.getImage();
			g2d.drawImage(image, xPos, yPos, io);
		}
		
		public boolean isDestroyed()
		{
			return destroyed;
		}
		
		public int getXPos()
		{
			return xPos;
		}
		
		public int getYPos()
		{
			return yPos;
		}
	}
}